//导入jwt
const jwt = require('jsonwebtoken'); 
//读取配置项
const {secret} = require('../config/config')
module.exports = (req, res, next) => {
  let token = req.get('token');
  if (!token) {
    return res.json({
      codde: '2003',
      msg: 'token缺失',
      data: null
    })
  }
  //校验token
  jwt.verify(token, secret,(err,data)=>{
    //检测token是否正确
    if(err){
      return res.json({
        code:'2004',
        msg:'token校验失败~~',
        data:null
      })
    }
    //保存用户信息
    req.user
    //如果 token 校验成功
    next();
  })
}